import { defineStore } from 'pinia'
import { login, logout } from '@/api/base/base'
import { setToken } from '@/utils/cookie'
import { useAppStore } from '@/store/modules/app'
import { getUserInfo } from '@/api/base/base'
import { ApiCode } from '@/enums/ApiCode'
export const useUserStore = defineStore('user', {
    state: () => {
        return {
            userInfo: {} as UserInfo
        }
    },
    getters: {},
    actions: {
        async Login(form: BaseUser): Promise<number>{
            try{
                const { code, data } = await login(form)
                console.log('userInfo')
                console.log(data)
                if(ApiCode.SUCCESS == code){
                    setToken(data)
                }
                return code
            }catch(err){
                console.error(err)
                return ApiCode.FAILED
            }
        },
        async Logout() {
            await logout()
            useAppStore().destory()
        },
        async InitUserInfo(){
            const { data } = await getUserInfo()
            this.userInfo = data
        }
    }
})
